import { AxiosError } from 'axios'
import { warn } from '@/services/alert-service'

export class ErrorResponse {
  code: string
  message: string
}

function handleErrorResponse (error: ErrorResponse): void {
  warn(error.message)
}

export default function handleError (e: any): void {
  const resp = e.response
  if (!resp) {
    throw e
  }
  const data = resp.data
  if (!data) {
    throw e
  }
  const { code, message } = data
  if (!code) {
    throw e
  }
  const error = new ErrorResponse()
  error.code = code
  error.message = message
  handleErrorResponse(error)
}
